
package validation;

import utility.cardHelper;
import card.Cardset;

/**
 * Card Combination - Trio-with-Pair
 * Valid:	3-3-3-4-4, 9-9-9-A-A
 * Invalid:	5-5-5-2-A, 3-3-4-4-5
 * 
 */
public class TrioWithPair extends Type {

	private static TrioWithPair instance;
	
	/**
	 *  Private Constructor
	 *  Set instance to null(default)
	 */
	private TrioWithPair(){
		super();
		instance = null;
	}

	/**
	 * Implementation of Singleton
	 * @return Type instance
	 */
	public static Type getInstance(){
		if(instance == null){
			instance = new TrioWithPair();
		}
		return instance;	
	}
	
	@Override
	public void clear(){
		instance = null;
	}
	
	@Override
	public Type identify(Cardset thisHand) {
		// TODO Auto-generated method stub
		int trioCard = 0;
		int pairCard = 0;
		boolean isValid = false;
		int n = thisHand.count();
		if(n == 5){
			int[] histogram = new int[16];
	        for(int i=0;i<histogram.length;i++){
	        	histogram[i] = 0;
	        }
	        for(int i=0;i<n;i++){
	        	histogram[cardHelper.value2priority(thisHand.getCard(i).getValue())]++;
	        }
        	for(int i=0;i<histogram.length;i++){
        		if(histogram[i] == 3){
        			trioCard++;
        		} else if(histogram[i] == 2){
        			pairCard++;
        		} 
        	}
        	if(trioCard == 1 && pairCard == 1)
        		isValid = true;
		}
		
		if(isValid)
			return getInstance();
		else
			return null;
	}

	@Override
	protected int getPriority(Cardset thisHand) {
		// TODO Auto-generated method stub
		int priority = 0;
		int[] histogram = new int[16];
        for(int i=0;i<histogram.length;i++){
        	histogram[i] = 0;
        }
        for(int i=0;i<thisHand.count();i++){
        	histogram[cardHelper.value2priority(thisHand.getCard(i).getValue())]++;
        }
    	for(int i=0;i<histogram.length;i++){
    		if(histogram[i] == 3){
    			priority = i;
    		}
    	}
		return priority;
	}

}
